package com.xigege.nio.bufferAndChannel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class NIOFileChannelTransferFromFile {
    public static void main(String[] args) throws IOException {
        //创建相关流
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\a.txt");
        FileInputStream fileInputStream = new FileInputStream("d:\\a1.txt");

        //获取各个流对应的FileChannel
        FileChannel sourceCh = fileInputStream.getChannel();
        FileChannel destCh = fileOutputStream.getChannel();

        //使用transferFrom完成拷贝
        destCh.transferFrom(sourceCh, 0, sourceCh.size());

        //关闭相关的流和通道
        sourceCh.close();
        destCh.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}
